package com.ibm.eg.cvmanager.dao.impl;

import java.util.ArrayList;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.ibm.eg.cvmanager.dao.util.HibernateUtil;
import com.ibm.eg.cvmanager.dto.Interview;

import exception.TechnicalException;
import exception.TechnicalException;

public class BaseDao {
	
	public void beginTransaction() {
			Session session =  HibernateUtil.getCurrentSession();
			session.beginTransaction();
		
	}
	public void commitTransaction() {
			Session session =  HibernateUtil.getCurrentSession();
			session.getTransaction().commit();
	}
	public void rollbackTransaction(){
		
			Session session =  HibernateUtil.getCurrentSession();
			session.getTransaction().rollback();
		
	}
	public void add(Object obj) throws TechnicalException{
		
		Session session =  HibernateUtil.getCurrentSession();
		try {
			session.persist(obj);
		} 
		catch (HibernateException re) {
			re.printStackTrace();
			throw new TechnicalException("add() error in BaseDao",re);
		}
	}
	
	public void delete(Object obj) throws TechnicalException{
		Session session =  HibernateUtil.getCurrentSession();
		try {
			
			session.delete(obj);
			
		} 
		catch (HibernateException re) {
			throw new TechnicalException("delete() error in BaseDao",re);
		}
	}
	
	public void update(Object obj) throws TechnicalException{
		Session session =  HibernateUtil.getCurrentSession();
		try {
			session.update(obj);	
		} 
		catch (HibernateException re) {
			throw new TechnicalException("update() error in BaseDao", re);
		}
	}
	
	public ArrayList getAll(Class clazz) throws TechnicalException{
		Session session =  HibernateUtil.getCurrentSession();
		try {
			ArrayList objects = (ArrayList<Interview>) session
				.createQuery("FROM "+ clazz.getName()).list();
			return objects;
		}
		catch(HibernateException ex)
		{
			throw new TechnicalException("getAll() error in BaseDao",ex);
		}
	}
}
